import axios from 'axios'

let _axios = axios.create({
    baseURL: 'http://81.71.132.153:12312',
})

_axios.interceptors.request.use(config=>{
    if(!config.headers){
        config.headers = {}
    }
    config.headers.t = localStorage.getItem('token')
    return config
})

_axios.interceptors.response.use(res=>{
    if(res?.data?.code !== 200){
        console.error(`[${res?.data.code}]${res?.data.msg}`)
        throw res.data
    }
    return res
})

let createAPI = ({route = '', raw = false} = {})=>new Proxy(()=>{}, {
    get(target, key){
        if(key === '$raw'){
            return createAPI({route, raw: true})
        }
        return createAPI({route: `${route}/${key}`})
    },
    apply(target, thisArg, args){
        let ret = _axios.post(route, ...args)
        if(raw){
            return ret
        }else{
            return ret.then(res=>res?.data?.data)
        }
    }
})

export default createAPI()